package com.foreknow.demo14;

import java.io.*;

/**
 * 将对象写到文件中，然后读取文件中的对象
 * 思考：用什么流来实现？ ObjectInputStream ObjectOutputStream
 */
public class ReadWriteObject {
    public static void main(String[] args) {

        try {
            //向文件中写入Student对象
            //            FileOutputStream fos = new FileOutputStream("abc.ser");
            //            ObjectOutputStream os = new ObjectOutputStream(fos);
            //            os.writeObject(new Student(100,"fender",20,"1212121212"));
            //            os.writeObject(new Student(101,"gibson",30,"3232323233"));
            //            os.close();
            FileInputStream fis = new FileInputStream("abc.ser");
            ObjectInputStream ois = new ObjectInputStream(fis);
            Student stu1 = (Student) ois.readObject();
            System.out.println(stu1.getName());
            Student stu2 = (Student) ois.readObject();
            System.out.println(stu2.getName());
            ois.close();

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
